package com.shop.cloud.controller.hongfu;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.shop.cloud.common.mybatis.BaseController;
import com.shop.cloud.domain.hongfu.DistributionMsg;
import com.shop.cloud.domain.hongfu.InviteHistory;
import com.shop.cloud.domain.hongfu.User;
import com.shop.cloud.filter.CurrentUserId;
import com.shop.cloud.model.response.ObjectDataResponse;
import com.shop.cloud.service.hongfu.IDistributionMsgService;
import com.shop.cloud.service.hongfu.IInviteHistoryService;
import com.shop.cloud.service.hongfu.IUserService;
import com.shop.cloud.util.Page;

import io.swagger.annotations.ApiOperation;

/**
 * 邀请有礼管理
 * @author Yan Weiwei
 *
 */
@RestController	
@RequestMapping(value = "api")
public class DistributionMsgController extends BaseController {
	
	@Autowired
	private IInviteHistoryService inviteHistoryService;
	
	@Autowired
	private IDistributionMsgService distributionMsgService;
	
	@Autowired
    private IUserService userService;

	@ApiOperation(value = "获取我的邀请列表", notes = "获取我的邀请列表")
	@RequestMapping(value = "/myInviteList", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<InviteHistory>> myInviteList(@CurrentUserId Long userId, @RequestBody InviteHistory inviteHistory){
		
		inviteHistory.setUserId(userId);
		inviteHistory.setOrderBy("INVITE_HISTORY.invite_time DESC");
		Page<InviteHistory> date = inviteHistoryService.getPage(inviteHistory);
		
		return new ObjectDataResponse<Page<InviteHistory>>(date);
	}
	
	@ApiOperation(value = "获取我的邀请总计", notes = "获取我的邀请总计")
	@RequestMapping(value = "/myInviteMsg", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<InviteHistory> myInviteMsg(@CurrentUserId Long userId, @RequestBody InviteHistory inviteHistory){
		
		inviteHistory.setUserId(userId);
		InviteHistory data = inviteHistoryService.handleInviteMsg(inviteHistory);
		
		return new ObjectDataResponse<InviteHistory>(data);
	}
	
	@ApiOperation(value = "获取我的邀请得利列表", notes = "获取我的邀请得利列表")
	@RequestMapping(value = "/myInviteMsgList", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<DistributionMsg>> myInviteMsgList(@CurrentUserId Long userId, @RequestBody DistributionMsg distributionMsg){
		
		distributionMsg.setToUser(userId);
		distributionMsg.setStatus(0);
		distributionMsg.setOrderBy("DISTRIBUTION_MSG.create_time DESC");
		Page<DistributionMsg> data = distributionMsgService.getPage(distributionMsg);
		List<DistributionMsg> list = data.getData();
		if(null != list && list.size() > 0){
			for(DistributionMsg msg : list){
				Long fromUser = msg.getFromUser();
				User user = userService.get(fromUser);
				msg.setInviteUserImg(user.getPortrait());
			}
		}
		data.setData(list);
		
		return new ObjectDataResponse<Page<DistributionMsg>>(data);
	}
}
